<template>
  <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" placeholder="请输入角色描述" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button size="small" @click="closeFn">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addRole, roleDetail, editRole } from '@/api/roles/index'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '', // 角色名称
        description: '' // 角色描述
      },
      rules: {
        title: [
          { required: true, message: '角色名称不能为空！', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空！', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取角色详情
    async getRoleDetail(id) {
      const res = await roleDetail(id)
      this.formData = res.data
      // console.log('编辑', this.formData)
    },
    // 确定  新增 / 编辑
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editRole(this.formData)
        console.log(this.formData)
      } else {
        await addRole(this.formData)
      }
      this.$emit('updateEvent')
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
      this.formData = {
        title: '',
        description: ''
      }
    }
  }
}
</script>
<style scoped>
</style>
